package com.bdqn.hospital.controller;

import com.bdqn.hospital.pojo.Department;
import com.bdqn.hospital.pojo.Registry;
import com.bdqn.hospital.pojo.Room;
import com.bdqn.hospital.service.DepartmentService;
import com.bdqn.hospital.service.RoomService;
import com.bdqn.hospital.service.impl.DepartmentServiceImpl;
import com.bdqn.hospital.service.impl.RegistryServiceImpl;
import com.bdqn.hospital.service.impl.RoomServiceImpl;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import com.bdqn.hospital.pojo.*;
import com.bdqn.hospital.service.PatientService;
import com.bdqn.hospital.service.RegistryService;
import com.bdqn.hospital.service.ScheduleService;
import com.bdqn.hospital.service.SysUsersService;
import com.bdqn.hospital.service.impl.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @version : 1.0
 * @Author :guogd
 * @createTime: 2023/03/01 14:38
 * @Description: com.bdqn.hospital.controller
 */
@Controller
@RequestMapping("registry")
public class RegistryController {
    /**
     * 挂号单Controller
     */
    @Autowired
    private RegistryServiceImpl registryService;
    @Autowired
    private SysUsersServiceImpl sysUsersService;
    @Autowired
    private PatientServiceImpl patientService;
    @Autowired
    private PrescriptionServiceImpl prescriptionService;
    @Autowired
    private ScheduleServiceImpl scheduleService;
    @Autowired
    private DepartmentServiceImpl departmentService;
    @Autowired
    private RoomServiceImpl roomService;
    @Autowired
    private MedicalAdviceServiceImpl medicalAdviceService;
    @Autowired
    private  PrescriptionDetailServiceImpl prescriptionDetailService;
    /**
     * 查询全部挂号信息
     * @param curPage
     * @param idCard
     * @param userName
     * @param status
     * @param model
     * @return
     */
    @RequestMapping("getAll")
    public String getAll(@RequestParam(value = "curPage",required = false,defaultValue = "1") Integer curPage,
                         @RequestParam(value = "idCard",required = false) String idCard,
                         @RequestParam(value = "userName",required = false) Integer userName,
                         @RequestParam(value = "status",required = false) Integer status,
                         Model model, HttpSession session){
        SysUsers user = (SysUsers) session.getAttribute("user");
        PageInfo<Registry> registryServiceAll = registryService.getAll(curPage, 5,idCard,userName,status,user.getId());
        model.addAttribute("idCard",idCard);
        model.addAttribute("userName",userName);
        model.addAttribute("status",status);
        model.addAttribute("doctorList",sysUsersService.getUserName());
        model.addAttribute("registryList",registryServiceAll);
        return "developerAppinfolist";
    }
    @RequestMapping("getPatientById")
    public String getPatientById(@RequestParam("pid") Integer pid,@RequestParam("rid") Integer rid,
                                 @RequestParam("isMedicare") Integer isMedicare,
                                 @RequestParam("scheduleId") Integer scheduleId
                     ,Model model){
        Patient patientById = patientService.getPatientById(pid);
        registryService.updStatus(rid,2,1);
        model.addAttribute("patient",patientById);
        model.addAttribute("registryId",rid);
        model.addAttribute("scheduleId",scheduleId);
        model.addAttribute("isMedicare",isMedicare);
        return "developerAppversionmodify";
    }

    /**
     * 点击问诊结束修改状态返回查询挂号单页面
     */
    @RequestMapping("registryDown")
    public String registryDown(@RequestParam("registryId") Integer registryId,
                               @RequestParam("scheduleId") Integer scheduleId){
        scheduleService.updCountAndTime(scheduleId);
        registryService.updStatus(registryId,3,1);
        return "redirect:getAll";
    }


    /**
     * 添加药方
     * @param prescriptionDetail
     * @param model
     * @throws IOException
     */
    @RequestMapping("addYaoFang")
    @ResponseBody
    public Prescription addYaoFang(PrescriptionDetail prescriptionDetail,
                                   @RequestParam("idss[]") List<Integer> idss,
                                   @RequestParam("countList[]") List<Integer> countList,
                                   Model model) throws IOException {
        prescriptionDetail.setIds(idss);
        prescriptionDetail.setAmountList(countList);
        Prescription prescription = prescriptionService.addPrescriptionDetail(prescriptionDetail);
        return prescription;
    }



    @RequestMapping("toRegistry")
    public String toRegistry(){
        return "developerAppinfoadd";
    }

    @RequestMapping("toAddRegistry")
    public String toAddRegistry(Model model){
        List<Department> departmentList =departmentService.findAll();
        List<Room> roomList = roomService.findAll();
        model.addAttribute("departmentList",departmentList);
        model.addAttribute("roomList",roomList);
        return "registryAdd";
    }

    @RequestMapping("addRegistry")
    public void addRegistry(Registry registry, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=UTF-8");
        //添加挂号单信息
        Integer row = registryService.addRegistry(registry);
        if(row>0){
            resp.getWriter().print("<script>alert('添加成功！');location.href='/registry/toRegistry'</script>");
        }else{
            resp.getWriter().print("<script>alert('添加失败！');history.back();</script>");
        }
    }

    @RequestMapping("toSelectAll")
    public String toSelectAll(@RequestParam(value = "curPage",required = false,defaultValue = "1") Integer curPage,
                              @RequestParam(value = "idCard",required = false) String idCard,
                              @RequestParam(value = "userName",required = false) Integer userName,
                              @RequestParam(value = "status",required = false) Integer status,
                              @RequestParam(value = "roomId",required = false) Integer roomId,
                              Model model, HttpSession session){
        SysUsers user = (SysUsers) session.getAttribute("user");
        PageInfo<Registry> registryServiceAll = registryService.getAll2(curPage, 5,idCard,userName,status,roomId,user.getId());
        model.addAttribute("idCard",idCard);
        model.addAttribute("userName",userName);
        model.addAttribute("status",status);
        List<Room> roomList = roomService.findAll();
        model.addAttribute("doctorList",sysUsersService.getUserName());
        model.addAttribute("registryList",registryServiceAll);
        model.addAttribute("roomList",roomList);
        model.addAttribute("roomId",roomId);
        return "developerAppinfolist_guahao";
    }

    @RequestMapping("getDateilInfo")
    public String getDateilInfo(Integer id,Model model){
        Registry registry = registryService.getPayById(id);//挂号信息
        Patient patient = patientService.getPatientById(registry.getPatientId());//病人信息
        Department department = departmentService.getById(registry.getDepartmentId());//就诊科室信息
        Room room= roomService.getById(registry.getRoomId());//就诊房间信息
        SysUsers user = sysUsersService.getById(registry.getUserId());//挂号医师信息
        MedicalAdvice medicalAdvice = medicalAdviceService.getByRegistryId(registry.getId());//遗嘱信息
        SysUsers doctor = sysUsersService.getById(medicalAdvice.getUserId());//就诊医师信息
        List<PrescriptionDetail> prescriptionDetailList = prescriptionDetailService.getByRegistryId(registry.getId());//药方信息
        model.addAttribute("registry",registry);
        model.addAttribute("patient",patient);
        model.addAttribute("room",room);
        model.addAttribute("department",department);
        model.addAttribute("user",user);
        model.addAttribute("medicalAdvice",medicalAdvice);
        model.addAttribute("doctor",doctor);
        model.addAttribute("prescriptionDetailList",prescriptionDetailList);
        return "QueryDetails";
    }

}
